package com.lagou.dao.impl;

import com.lagou.annotation.MyAutowire;
import com.lagou.annotation.MyComponent;
import com.lagou.dao.AccountDao;
import com.lagou.utils.ConnectionUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@MyComponent(value = "accountDao", flag = true)
public class AccountDaoImpl implements AccountDao {

    @MyAutowire("connectionUtil")
    private ConnectionUtil connectionUtil;

    @Override
    public void updateBalanceByUsername(String username, Double money) {
        try {
            Connection connection = connectionUtil.getConnection();
            String sql = "update account set balance = (balance + ?) where username = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1, money);
            preparedStatement.setObject(2, username);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
